package com.zhudi.chart;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.zhudi.chart.view.ChartView;
import com.zhudi.chart.view.ChartViewY;

public class MainActivity extends Activity {
	private Context context;
	private LinearLayout lilyTu;
	private ChartView viewWeek;
	private ChartViewY viewWeekY;
	private LinearLayout lilyChart;
	private LinearLayout lilyChartY;
	private String[] data = new String[]{"3.1","6.3","1.1","6.7","7.9","7.2","3.1","9.2","2.1"};
	private String[] xweek = new String[]{"01:01:00","01:02:00","01:03:00","01:04:00","01:05:00","01:06:00","01:07:00","01:08:00","01:09:00"};
	private String[] yweek = new String[5];
	private int height = 200;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;
		initView();
	}

	@SuppressLint("NewApi")
	private void initView() {
		lilyTu = (LinearLayout) findViewById(R.id.lilyTu);
		lilyChart = (LinearLayout) findViewById(R.id.lilyChart);
		lilyChartY = (LinearLayout) findViewById(R.id.lilyChartY);
		viewWeek = new ChartView(context);
		viewWeekY = new ChartViewY(context);
		((HorizontalScrollView) findViewById(R.id.hsChart)).setOverScrollMode(View.OVER_SCROLL_NEVER);	
		
		 setChartView();
	}

	private void setChartView() {
		float maxWeek = 0.0f;
		for (int i = 0; i < data.length; i++) {
			float all = Float.parseFloat(data[i]);
			if (all > maxWeek) {
				maxWeek = all;
			}
		}
		yweek[0] = "0.0";//Y轴最小值
		for (int j = 1; j < yweek.length - 1; j++) {
			yweek[j] = String.valueOf(maxWeek / 4f * j);
		}
		yweek[4] = String.valueOf(maxWeek);//设置Y轴最大值
		setView(viewWeek,lilyChart,xweek,yweek,data,true);
		//设置Y轴值 
		setViewY(viewWeekY, lilyChartY,xweek, yweek, data,true);
	}
	
	/**
	 *  ChartView 
	 * @param xweek
	 * @param yweek
	 * @param dataweek
	 */
	private void setView(ChartView view,LinearLayout linearLayout, String[] xweek, String[] yweek, String[] data,boolean flag) {
		view.SetInfo(xweek, yweek, data,getWindowManager().getDefaultDisplay(),
				height,
				height,flag);
		view.invalidate();
		linearLayout.removeAllViews();
		linearLayout.addView(view);
	}
	
	/**
	 * ChartViewY
	 * @param xweek
	 * @param yweek
	 * @param dataweek
	 * getResources().getInteger(R.integer.table_position)
	 */
	private void setViewY(ChartViewY view,LinearLayout linearLayout,String[] xweek, String[] yweek, String[] dataweek,boolean flag) {
		view.SetInfo(xweek, yweek, dataweek, getWindowManager().getDefaultDisplay(),
				height,
				height,flag);
		view.invalidate();
		linearLayout.removeAllViews();
		linearLayout.addView(view);
	}
	
//	@Override
//	protected void onResume() {
//		super.onResume();
//		ViewTreeObserver vto = lilyTu.getViewTreeObserver();
//        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//            public void onGlobalLayout() {
//            	height = lilyTu.getHeight() - 50;
//            	System.out.println(height);
//            	lilyTu.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//            }
//        });
//	}

}
